package com.geovis.emergency.spd.biz.zwdd.config.schedule.handler;


import com.alibaba.xxpt.gateway.shared.client.http.ExecutableClient;
import com.geovis.emergency.spd.biz.zwdd.config.schedule.AbsScheduleHandler;
import com.geovis.emergency.spd.biz.zwdd.handler.ZwddSynchronizeHandler;
import com.geovis.emergency.spd.biz.zwdd.props.ZwddProperties;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.Duration;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 政务钉钉通讯录同步
 *
 * @author linrf
 * @version V1.0
 * @date 2022/7/4 20:00
 */
@Slf4j
@Data
@EqualsAndHashCode(callSuper = true)
@Configuration
@ConfigurationProperties(prefix = "scheduling.zwdd-sync")
@ConditionalOnProperty(prefix = "scheduling.zwdd-sync", name = "enable", havingValue = "true")
public class ZwddSyncSchedule extends AbsScheduleHandler {

    /**
     * 是否启动
     */
    private final AtomicBoolean isRunning = new AtomicBoolean(false);

    /**
     * 海证通得请求客户端
     */
    private final ExecutableClient executableClient;
    /**
     * 海证通的配置
     */
    private final ZwddProperties zwddProperties;

    /**
     * 是否启动
     */
    private boolean enable = false;

    /**
     * 删除未更新数据时回退的时间,默认7天
     */
    private Duration rmBackTime = Duration.ofDays(7);

    /**
     * 批量获取的最大数量
     */
    private Integer maxSize = 64;

    /**
     * 批量保存最大数量
     */
    private Integer batchMaxSize = 1000;


    private String cron;

    @Autowired
    public ZwddSyncSchedule(ExecutableClient executableClient, ZwddProperties zwddProperties) {
        this.executableClient = executableClient;
        this.zwddProperties = zwddProperties;
    }


    /**
     * 定时同步更新
     * // @Scheduled(cron = "0/2 * * * * ? ", fixedDelayString = "PT2S")
     * // @Scheduled(fixedDelayString = "P1DT1H")
     */

    @Scheduled(cron = "${scheduling.zwdd-sync.cron}")
//    @Scheduled(cron = "0/2 * * * * ? ")
    public void syncUpdate() {
        if (!isRunning.compareAndSet(false, true)) {
            // 正在同步
            log.warn("发现同步正在执行！跳过本次执行");
            return;
        }
        try {
            new ZwddSynchronizeHandler(executableClient, zwddProperties, rmBackTime, maxSize, batchMaxSize).synchronize(false, false);
        } finally {
            // 结束同步
            isRunning.set(false);
        }
    }

//    /**
//     * 定时全量同步更新
//     * // @Scheduled(cron = "0/2 * * * * ? ", fixedDelayString = "PT2S")
//     * // @Scheduled(fixedDelayString = "P1DT1H")
//     */
//    @Scheduled(cron = "0 0 2 * * FRI ")
//    public void syncUpdateDept() {
//        if (!isRunning.compareAndSet(false, true)) {
//            // 正在同步
//            log.warn("发现同步正在执行！跳过本次执行");
//            return;
//        }
//        try {
//            new ZwddSynchronizeHandler(executableClient, zwddProperties, rmBackTime, maxSize, batchMaxSize).synchronize(true, false);
//        } finally {
//            // 结束同步
//            isRunning.set(false);
//        }
//    }
//
//    /**
//     * 定时全量同步更新
//     * // @Scheduled(cron = "0/2 * * * * ? ", fixedDelayString = "PT2S")
//     * // @Scheduled(fixedDelayString = "P1DT1H")
//     */
//    @Scheduled(cron = "0 0 3 1 * ? ")
//    public void syncUpdateAll() {
//        if (!isRunning.compareAndSet(false, true)) {
//            // 正在同步
//            log.warn("发现同步正在执行！跳过本次执行");
//            return;
//        }
//        try {
//            new ZwddSynchronizeHandler(executableClient, zwddProperties, rmBackTime, maxSize, batchMaxSize).synchronize(true, true);
//        } finally {
//            // 结束同步
//            isRunning.set(false);
//        }
//    }


}
